package com.djl.mymall.mbg.service.impl;


import com.djl.mymall.dao.UmsAdminRoleRelationDao;
import com.djl.mymall.mbg.entity.Admin;
import com.djl.mymall.mbg.entity.Permission;
import com.djl.mymall.mbg.mapper.AdminMapper;
import com.djl.mymall.mbg.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djl.mymall.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author djl
 * @since 2022-01-06
 */
@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private UmsAdminRoleRelationDao umsAdminRoleRelationDao;
    /**
     *  根据用户名查询用户
     * @param username
     * @return
     */
    @Override
    public Admin getAdminByUsername(String username) {
        Admin admin=adminMapper.getAdminByUsername(username);
        if (admin==null){
            return null;
        }
        return  admin;
    }

    @Override
    public Admin register(Admin adminParam) {
        if (getAdminByUsername(adminParam.getUsername())==null){
            return null;
        }
        adminParam.setCreateTime(new Date());
        adminParam.setStatus(1);
        String adminPassword=passwordEncoder.encode(adminParam.getPassword());
        adminParam.setPassword(adminPassword);
        adminMapper.register(adminParam);

        return adminParam;
    }

    @Override
    public String login(String username, String password) {
        String token=null;
        try {
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            if (!passwordEncoder.matches(password,userDetails.getPassword())){
                throw  new BadCredentialsException("密码不正确");
            }
            UsernamePasswordAuthenticationToken authentication=new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            token= jwtTokenUtil.generateToken(userDetails);

        } catch (Exception e) {
            log.info("登录异常：{}",e.getMessage());
            e.printStackTrace();
        }
        return token;
    }

    @Override
    public List<Permission> getPermissionList(Long adminId) {
        List<Permission> permissionList = umsAdminRoleRelationDao.getPermissionList(adminId);
        if (null==permissionList){
            return null;
        }
        return permissionList;
    }
}
